<?php

	require_once("fta.201003310000/fta.php");
	fta_registerModules(
		array(
		"cms.201003310000/config/cmsConfig",//we will deprecate the old cms config soon..
		"FTAWebPage/ftaDefaultWidgets"
	));	

class MAIExhibition extends FTAObject {
		
		var $renderExhibitionFunction;
		
		function MAIExhibition(){
			parent::__construct();
			
			global $db;	
			$this->cmsDocument = new CmsDocument($db);	
		}
		
		function getExhibition($exhibition){
			
			//add additional cache checks here before we do all this shit..
			if (is_null($exhibition))
				return false;
				
			$this->cmsDocument->flush();
			$this->cmsDocument->setActiveDocuments();
			$this->cmsDocument->addDocumentType('docExhibit');
			
			//Limit the classes displayed to those that have spaces
			//$this->cmsDocument->addConstraint("authordepartment", 0, ">");
			
			if (is_numeric( ($exhibition * 1) ) &&  0 < ($exhibition * 1)){
				$this->cmsDocument->addConstraint('documentcode',$exhibition);	
			} else {
				$this->cmsDocument->addConstraint('<<title>>',$exhibition);
			}
			
			$this->cmsDocument->setPageSize(1);
			
			$this->cmsDocument->resetIterator();
			
			if($this->cmsDocument->totalDocuments() == 1) {
				return $this->cmsDocument->nextDocumentFromCache();
			} else {
				return false;
			}
		}
		
		function getExhibitions($exhibitionList = null, $pageSize = null, $pageNumber = null, $sort = null, $direction = null){
			
			$this->cmsDocument->flush();
			$this->cmsDocument->setActiveDocuments();
			$this->cmsDocument->addDocumentType('docExhibit');
			
			if($exhibitionList == "Coming-Exhibitions"){
				$this->cmsDocument->addConstraint("startdate", date('Y-m-d H:i:s'), ">");
			} elseif($exhibitionList == "Currently-Showing"){
				$this->cmsDocument->addConstraint("startdate", date('Y-m-d H:i:s'), "<=");
				$this->cmsDocument->addConstraint("enddate", date('Y-m-d H:i:s'), ">=");
			} elseif($exhibitionList == "Past-Exhibitions"){
				$this->cmsDocument->addConstraint("enddate", date('Y-m-d H:i:s'), "<");
			}
			
			if(!is_null($sort)){
				if(!is_null($direction)){
					$this->cmsDocument->addSort($sort, $direction);
				} else {
					$this->cmsDocument->addSort($sort);
				}
			} else {
				if($exhibitionList == "Past-Exhibitions"){
					$this->cmsDocument->addSort("enddate", "DESC");
				} else {
					$this->cmsDocument->addSort("startdate");
				}
			}
			
			$this->cmsDocument->setPageSize($pageSize);
			$this->cmsDocument->setPageNumber($pageNumber);
			
			$this->cmsDocument->resetIterator();
			
			$allDocuments = $this->cmsDocument->getAllDocuments();
			
			if (!$allDocuments)
				$allDocuments = array();
			
			//echo "Query = " . $this->cmsDocument->documentIteratorQuery() . "<br />";
			//echo "Exhibitions = <pre>";print_r($allDocuments);echo "</pre>";
			
			return $allDocuments;	
		}	
		
		function getExhibitionForEnrolment($pageSize = null, $pageNumber = null, $sort = null, $direction = null){
			
			$this->cmsDocument->flush();
			$this->cmsDocument->setActiveDocuments();
			$this->cmsDocument->addDocumentType('docExhibit');
			
			$this->cmsDocument->addConstraint('extra13', '1');
			
			$this->cmsDocument->addConstraint('documentyear', date("Y-m-d"), ">=");
			
			if(!is_null($sort)){
				if(!is_null($direction)){
					$this->cmsDocument->addSort($sort, $direction);
				} else {
					$this->cmsDocument->addSort($sort);
				}
			} else {
				$this->cmsDocument->addSort("startdate");
			}
			
			$this->cmsDocument->setPageSize($pageSize);
			$this->cmsDocument->setPageNumber($pageNumber);
			
			$this->cmsDocument->resetIterator();
			
			$allDocuments = $this->cmsDocument->getAllDocuments();
			
			if (!$allDocuments)
				$allDocuments = array();
			
			//echo "Query = " . $this->cmsDocument->documentIteratorQuery() . "<br />";
			//echo "Exhibitions = <pre>";print_r($allDocuments);echo "</pre>";
			
			return $allDocuments;	
		}	
		
		function getExhibitionEnrolmentOptions($productId = null, $pageSize = null, $pageNumber = null, $sort = null, $direction = null){
			$exhibitions = $this->getExhibitionForEnrolment($pageSize, $pageNumber, $sort, $direction);
			
			foreach ($exhibitions as $exhibition){
				echo "<option value='" . $exhibition->documentcode . "'";
				if($exhibition->documentcode == $productId){
					echo " selected ";
				}
				echo " >". $exhibition->title . "</option>";
			}
		}
		
		function renderExhibitionList($exhibitionList = null, $pageSize = null, $pageNumber = null, $sort = null, $direction = null){
			// 1.  Get exhibitions
			$exhibitions = $this->getExhibitions($exhibitionList, $pageSize, $pageNumber, $sort, $direction);
			if(count($exhibitions) < 1){
				return false;
			}
			
			if(is_null($this->renderExhibitionFunction)) {
				
				global $MAIExhibitionItemRenderFunction;
				if(!isset($MAIExhibitionItemRenderFunction)){
					$MAIExhibitionItemRenderFunction = '$o=mai_getSharedExhibitionObject(); echo $o->renderExhibitionItemFunction($item);';
				}
				//echo "this renderExhibitionFunction = '" . $this->renderExhibitionFunction . "' ";
				$this->renderExhibitionFunction  = create_function('$item', $MAIExhibitionItemRenderFunction );
			}
				
			
			
			//we need to map the links and do some funky scope wrandling..
			mai_exhibition_widgetList($exhibitions,"exhibition-list", null, null, null,$this->renderExhibitionFunction);
		}
		
		function renderExhibitionItemFunction(){
			
		}
		
		function getExhibitionDatesForCalendar(){
			$exhibitions = $this->getExhibitions();
			
			$exhibitionDates = array();
			
			foreach ($exhibitions as $item){
				//Determine exhibition range for link
				$exhibitionRange = '';
				if($item->startdate > date('Y-m-d H:i:s')){
					$exhibitionRange = 'Coming-Exhibitions';
				} elseif($item->enddate < date('Y-m-d H:i:s')) {
					$exhibitionRange = 'Past-Exhibitions';
				} else {
					$exhibitionRange = 'Currently-Showing';
				}
				
				//Set values in array
				if(isset($exhibitionDates[date("m/d/Y", strtotime($item->startdate))])){
					$exhibitionDates[date("m/d/Y", strtotime($item->startdate))] .= " <a href='/Exhibitions/" . $exhibitionRange . "/' class='tip_link_exhibition' >" . $item->title . "</a> ";
				} else {
					$exhibitionDates[date("m/d/Y", strtotime($item->startdate))] = " <a href='/Exhibitions/" . $exhibitionRange . "/' class='tip_link_exhibition' >" . $item->title . "</a> ";
				}
			}
			return $exhibitionDates;
		}
		
}


	function mai_getExhibitionEnrolmentOptions($productId = null, $pageSize = null, $pageNumber = null, $sort = null, $direction = null){		
		ob_start();
		$exhibitionObject = mai_getSharedExhibitionObject();
		$exhibitionObject->getExhibitionEnrolmentOptions($productId, $pageSize, $pageNumber, $sort, $direction);
		return ob_get_clean();				
	}

	function mai_renderExhibitionList($exhibitionList = null, $pageSize = null, $pageNumber = null, $sort = null, $direction = null){		
		ob_start();
		$exhibitionObject = mai_getSharedExhibitionObject();
		$exhibitionObject->renderExhibitionList($exhibitionList, $pageSize, $pageNumber, $sort, $direction);
		return ob_get_clean();				
	}
	
	function mai_getExhibitionDatesForCalendar(){
		$exhibitionObject = mai_getSharedExhibitionObject();
		return $exhibitionObject->getExhibitionDatesForCalendar();
	}

	function mai_getSharedExhibitionObject(){

		global $fta_;

		global $MAIExhibitionClass;
		
		if(!is_null($MAIExhibitionClass) && "MAIExhibition" != $MAIExhibitionClass){
		} else {
			$MAIExhibitionClass = "MAIExhibition";
		}
			
		if (!array_key_exists('maiExhibition',$fta_)){
			$fta_->maiExhibition = new  $MAIExhibitionClass();
		}
		return $fta_->maiExhibition;
	}

	function mai_exhibition_widgetList($items,$listClass="list",$altItemClass='altItem',$firstItemClass='firstItem', $lastItemClass='lastItem',$renderItemFunction) {
?>
<ul class="<?= $listClass ?>">
<?		
	$row = -1;
	$numRows = count($items);
	
	foreach($items as $item){
		
		$itemClasses=array();
		
		if ($altItemClass && 0 == ($row++) % 2 ) 
			array_push($itemClasses,$altItemClass);
		
		if ($firstItemClass && 0 == $row)
			array_push($itemClasses,$firstItemClass);			
			
		if ($lastItemClass && $numRows -1 == $row)
			array_push($itemClasses,$lastItemClass);

?>		
		<li <?= ( 0 < count($itemClasses) ? "class=\"" . implode(" ",$itemClasses) . "\"" : "") ?> ><?= $renderItemFunction( $item ) ?></li>		
<?		
	}
?>
</ul>
<?		
	}
?>